package com.siyu.tickets.manager.impl;

import java.math.BigDecimal;
import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.siyu.common.hibernate3.Updater;
import com.siyu.common.page.Pagination;
import com.siyu.tickets.dao.OrderDao;
import com.siyu.tickets.dao.PartnerDao;
import com.siyu.tickets.dao.PayFlowDao;
import com.siyu.tickets.dao.ProductDao;
import com.siyu.tickets.dao.SupplierDao;
import com.siyu.tickets.entity.Order;
import com.siyu.tickets.entity.Partner;
import com.siyu.tickets.entity.PayFlow;
import com.siyu.tickets.entity.Product;
import com.siyu.tickets.entity.Supplier;
import com.siyu.tickets.entity.Ticket;
import com.siyu.tickets.manager.OrderMng;
import com.siyu.tickets.util.DefaultTicketCodeGenenator;
import com.siyu.tickets.web.ThreadVariable;

@Service
@Transactional
public class OrderMngImpl implements OrderMng {

	public Pagination getPage(String name, Date beginTime, Date endTime, Boolean valid, int pageNo, int pageSize) {
		Pagination page = dao.getPage(name, beginTime, endTime, valid, pageNo, pageSize);
		return page;
	}

	@Transactional(readOnly = true)
	public Order findById(Integer id) {
		Order entity = dao.findById(id);
		return entity;
	}

	public void newOrder(Order bean) {
		Date now = new Date();

		Product product = productDao.findById(bean.getProduct().getId());
		Partner partner = partnerDao.findById(ThreadVariable.getUser().getId());
		Supplier supplier = supplierDao.findById(product.getSupplier().getId());

		BigDecimal price = product.getPartnerPrice();
		BigDecimal money = price.multiply(new BigDecimal(bean.getQuantity()));

		// 校验分销商余额
		if (partner.getMoney().subtract(money).signum() < 0) {
			throw new RuntimeException("分销商余额不足，请充值后再下单！");
		}

		// order
		bean.setIsPay(true);
		bean.setCreateTime(now);
		bean.setPayTime(now);
		bean.setValid(true);
		bean.setAllowRefund(true);

		bean.setProduct(product);
		bean.setPartner(partner);

		bean.setPrice(price);// 单价
		bean.setMoney(money);// 总价

		// ticket
		Ticket ticket = new Ticket();
		ticket.setOrder(bean);
		ticket.setCode(DefaultTicketCodeGenenator.generate());
		ticket.setExpireTime(product.getExpireTime());
		ticket.setIsSend(true);
		ticket.setMoney(money);

		ticket.setCreateTime(now);
		ticket.setSendTime(now);
		ticket.setPartner(partner);
		ticket.setProduct(product);
		ticket.setSupplier(supplier);
		ticket.setValid(true);

		bean.getTickets().add(ticket);

		bean = dao.save(bean);

		// 扣除分销商余额
		partner.setMoney(partner.getMoney().subtract(money));
		partnerDao.updateByUpdater(new Updater<Partner>(partner));
		// 增加供应商的余额
		supplier.setMoney(supplier.getMoney().add(money));
		supplierDao.updateByUpdater(new Updater<Supplier>(supplier));

		// payFlow 分销商支付流水
		PayFlow consumeFlow = new PayFlow();
		consumeFlow.setAction("pay");
		consumeFlow.setCreateTime(now);
		consumeFlow.setMoney(money.negate());
		consumeFlow.setOperator(partner.getUser());
		consumeFlow.setUser(partner.getUser());
		consumeFlow.setOrder(bean);
		payFlowDao.save(consumeFlow);

		// payFlow 资源商收入流水
		PayFlow incomeFlow = new PayFlow();
		incomeFlow.setAction("income");
		incomeFlow.setCreateTime(now);
		incomeFlow.setMoney(money);
		incomeFlow.setOperator(partner.getUser());
		incomeFlow.setUser(supplier.getUser());
		incomeFlow.setOrder(bean);
		payFlowDao.save(incomeFlow);
		// sms
	}

	public Order updateOrder(Order bean) {
		Order oldBean = dao.findById(bean.getId());
		// TODO
		Updater<Order> updater = new Updater<Order>(oldBean);
		Order entity = dao.updateByUpdater(updater);
		return entity;
	}

	public Order deleteById(Integer id) {
		return dao.deleteById(id);
	}

	public Order[] deleteByIds(Integer[] ids) {
		Order[] beans = new Order[ids.length];
		for (int i = 0, len = ids.length; i < len; i++) {
			beans[i] = deleteById(ids[i]);
		}
		return beans;
	}

	@Autowired
	private OrderDao dao;

	@Autowired
	private ProductDao productDao;

	@Autowired
	private SupplierDao supplierDao;

	@Autowired
	private PartnerDao partnerDao;

	@Autowired
	private PayFlowDao payFlowDao;

}
